package com.pybox.spec;

import com.pybox.entity.TDept;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TDept Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TDeptSpec implements Specification<TDept> {

    private Map<String, String> params;

    public TDeptSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TDept> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String parentId = this.params.get("parentId");
        String ancestors = this.params.get("ancestors");
        String deptName = this.params.get("deptName");
        String orderNum = this.params.get("orderNum");
        String leader = this.params.get("leader");
        String phone = this.params.get("phone");
        String email = this.params.get("email");
        String status = this.params.get("status");
        String delFlag = this.params.get("delFlag");
        String createBy = this.params.get("createBy");
        String ctime = this.params.get("ctime");
        String updateBy = this.params.get("updateBy");
        String utime = this.params.get("utime");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(parentId)) {
            ps.add(criteriaBuilder.like(root.get("parentId"), "%" + parentId + "%"));
        }
        if (!StringUtils.isEmpty(ancestors)) {
            ps.add(criteriaBuilder.like(root.get("ancestors"), "%" + ancestors + "%"));
        }
        if (!StringUtils.isEmpty(deptName)) {
            ps.add(criteriaBuilder.like(root.get("deptName"), "%" + deptName + "%"));
        }
        if (!StringUtils.isEmpty(orderNum)) {
            ps.add(criteriaBuilder.like(root.get("orderNum"), "%" + orderNum + "%"));
        }
        if (!StringUtils.isEmpty(leader)) {
            ps.add(criteriaBuilder.like(root.get("leader"), "%" + leader + "%"));
        }
        if (!StringUtils.isEmpty(phone)) {
            ps.add(criteriaBuilder.like(root.get("phone"), "%" + phone + "%"));
        }
        if (!StringUtils.isEmpty(email)) {
            ps.add(criteriaBuilder.like(root.get("email"), "%" + email + "%"));
        }
        if (!StringUtils.isEmpty(status)) {
            ps.add(criteriaBuilder.like(root.get("status"), "%" + status + "%"));
        }
        if (!StringUtils.isEmpty(delFlag)) {
            ps.add(criteriaBuilder.like(root.get("delFlag"), "%" + delFlag + "%"));
        }
        if (!StringUtils.isEmpty(createBy)) {
            ps.add(criteriaBuilder.like(root.get("createBy"), "%" + createBy + "%"));
        }
        if (!StringUtils.isEmpty(ctime)) {
            ps.add(criteriaBuilder.like(root.get("ctime"), "%" + ctime + "%"));
        }
        if (!StringUtils.isEmpty(updateBy)) {
            ps.add(criteriaBuilder.like(root.get("updateBy"), "%" + updateBy + "%"));
        }
        if (!StringUtils.isEmpty(utime)) {
            ps.add(criteriaBuilder.like(root.get("utime"), "%" + utime + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}